//
//  SchoolRecordBarChart.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/7.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "SchoolRecordBarChart.h"
#import "SchoolRecordBarChartLabel.h"
#import "SchoolRecordBar.h"
@interface SchoolRecordBarChart ()
{
    UIScrollView *myScrollView;
}
@end
@implementation SchoolRecordBarChart{
    NSMutableArray *_chartLabelsForX;
    NSMutableArray *_chartLabelsForY;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.clipsToBounds = YES;
        myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(LLYLabelwidth, 0, frame.size.width-LLYLabelwidth, frame.size.height)];
        [self addSubview:myScrollView];
    }
    return self;
}

-(void)setYValues:(NSArray *)yValues
{
    _yValues = yValues;
    [self setYLabels:yValues];
}

-(void)setYLabels:(NSArray *)yLabels
{
    _chartLabelsForY = [NSMutableArray new];
    NSInteger max = 0;
    NSInteger min = 1000000000;
    for (NSArray * ary in yLabels) {
        for (NSString *valueString in ary) {
            NSInteger value = [valueString integerValue];
            if (value > max) {
                max = value;
            }
            if (value < min) {
                min = value;
            }
        }
    }
    if (max < 5) {
        max = 5;
    }
    _yValueMin = 0;
    _yValueMax = (int)max;
    
    if (_chooseRange.max!=_chooseRange.min) {
        _yValueMax = _chooseRange.max;
        _yValueMin = _chooseRange.min;
    }
    int num = 9.0;
    float level = (_yValueMax - _yValueMin) /(num - 1);
    CGFloat chartCavanHeight = self.frame.size.height - LLLabelHeight*3;
    CGFloat levelHeight = chartCavanHeight /(num - 1);
    
    for (int i=0; i< num; i++) {
        SchoolRecordBarChartLabel * label = [[SchoolRecordBarChartLabel alloc] initWithFrame:CGRectMake(0.0,chartCavanHeight - i*levelHeight + 5, LLYLabelwidth, LLLabelHeight)];
        label.textColor = rgba(142, 142, 142, 1);
        label.font = RFontWithSize(12);
        label.text = [NSString stringWithFormat:@"%.f",level * i+_yValueMin];
        [self addSubview:label];
        [_chartLabelsForY addObject:label];
    }
}

-(void)setXLabels:(NSArray *)xLabels
{
    _chartLabelsForX = [NSMutableArray new];
    
    _xLabels = xLabels;
    NSInteger num;
    if (xLabels.count>=8) {
        num = 8;
    }else if (xLabels.count<=4){
        num = 4;
    }else{
        num = xLabels.count;
    }
    _xLabelWidth = myScrollView.frame.size.width/num;
    
    for (int i=0; i<xLabels.count; i++) {
        SchoolRecordBarChartLabel *label = [[SchoolRecordBarChartLabel alloc] initWithFrame:CGRectMake((i *  _xLabelWidth ), self.frame.size.height - LLLabelHeight, _xLabelWidth, LLLabelHeight)];
        label.text = xLabels[i];
        label.textColor = rgba(142, 142, 142, 1);
        label.font = RFontWithSize(10);
        [myScrollView addSubview:label];
        [_chartLabelsForX addObject:label];
    }
    
    float max = (([xLabels count]-1)*_xLabelWidth + chartMargin)+_xLabelWidth;
    if (myScrollView.frame.size.width < max-10) {
        myScrollView.contentSize = CGSizeMake(max, self.frame.size.height);
    }
    double lineWidth = myScrollView.contentSize.width > myScrollView.width?myScrollView.contentSize.width:myScrollView.width;
    for (int i = 0; i < _chartLabelsForY.count; i++) {
        UILabel *Ylabel = _chartLabelsForY[i];
        
        UIView *line = [[UIView alloc] initWithFrame: CGRectMake(0, Ylabel.y + Ylabel.height/2.0 , lineWidth, 1)];
        line.backgroundColor = rgba(229, 229, 229, 1);
        [myScrollView addSubview:line];
    }
   
}

-(void)setColors:(NSArray *)colors
{
    _colors = colors;
}

- (void)setChooseRange:(CGRange)chooseRange
{
    _chooseRange = chooseRange;
}

-(void)strokeChart
{
    //self.backgroundColor = [UIColor blueColor];
    CGFloat chartCavanHeight = self.frame.size.height - LLLabelHeight*3;
    
    for (int i=0; i<_yValues.count; i++) {
        if (i==2)
            return;
        NSArray *childAry = _yValues[i];
        for (int j=0; j<childAry.count; j++) {
            NSString *valueString = childAry[j];
            UILabel *labelX = _chartLabelsForX[j];
            float value = [valueString floatValue];
            float grade = ((float)value-_yValueMin) / ((float)_yValueMax-_yValueMin);
            self.space = 0.02*_xLabelWidth;
            SchoolRecordBar * bar = [[SchoolRecordBar alloc] initWithFrame:CGRectMake( (j+(_yValues.count==1?0.1:0.05))*_xLabelWidth +i*_xLabelWidth * 0.37  , LLLabelHeight, _xLabelWidth * (_yValues.count==1?0.8:0.25), chartCavanHeight)];
            bar.number = [NSString stringWithFormat:@"%@",valueString];
            if(i == 0)
            {
                bar.center = CGPointMake(labelX.center.x - self.space/2.0 - _xLabelWidth*0.125, bar.center.y);
            }else{
                bar.center = CGPointMake(labelX.center.x + self.space/2.0 +  _xLabelWidth*0.125, bar.center.y);
            }
           
            bar.barColor = [_colors objectAtIndex:i];
            bar.gradePercent = grade;
            [myScrollView addSubview:bar];
            
        }
    }
}

- (NSArray *)chartLabelsForX
{
    return _chartLabelsForX;
}
- (NSArray *)chartLabelsForY
{
    return _chartLabelsForY;
}

@end
